package controler;

import java.util.HashMap;
import java.util.Map;
import model.Academy;
import model.Draft;
import model.beans.MinutaBean;
import model.beans.UsuarioBean;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import controler.forms.SelAcademyForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import business.Minuta;
import business.Usuario;

public class ActionService extends DispatchAction {
	private final String DRAFT = "bean_minuta";
    public ActionForward validateLogin(
    		ActionMapping mapping, 
    		ActionForm form, 
    		HttpServletRequest request, 
    		HttpServletResponse response) throws Exception {
    	
    	controler.forms.LoginForm forma = (controler.forms.LoginForm) form;
    	HttpSession session = request.getSession();
    	UsuarioBean ub_valida = null;
    	if ( ( ub_valida = Usuario.usuarioValido(forma.getStr_login(), forma.getStr_password()) ) != null ) {
        	session.setAttribute("ses_usuario", ub_valida);
            return mapping.findForward("login_success");
        } else {
            request.setAttribute("msg_error", getResources(request).getMessage("Login.Error.UserPassInvalid"));
            return mapping.findForward("login_fail");
        }
    }
    
    
    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward selectAcademy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	SelAcademyForm academy = (SelAcademyForm)form;
    	HttpSession session = request.getSession();
    	UsuarioBean user_log = (UsuarioBean) session.getAttribute("ses_usuario");
    	if( user_log == null)return mapping.findForward("session_invalid");
    	if( academy.getStr_academyCode() != null ){
    		String str_academyCode = academy.getStr_academyCode();
    		user_log.setAcademia(Academy.instance(str_academyCode));
    		session.setAttribute("ses_usuario", user_log);
    	}else{
    		if( user_log.getAcademia() != null ){
    			academy.setStr_academyCode(user_log.getAcademia().getCodigo());
    			form = academy; 
    		}
    	}
    	request.setAttribute("req_academies", Academy.list(user_log.getId()));
    	return mapping.findForward("default");
    }
    
    public ActionForward closeDrafts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	HttpSession session = request.getSession();
		MinutaBean minuta_bean = (MinutaBean) session.getAttribute(DRAFT);
		Map map = new HashMap();
		map.put("int_min", new Integer(minuta_bean.getInt_tk()));
		map.put("bol_cerrar", new Boolean(true));
		model.Draft.save(map);
		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessClose"));
    	return mapping.findForward("default");
    }
    
    public ActionForward listDrafts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ActionForward action_retVal = Validate.validateSession(mapping, request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	HttpSession session = request.getSession();
    	UsuarioBean user_log = (UsuarioBean) session.getAttribute("ses_usuario");
    	
    	String str_academyCode = request.getParameter("academy_id");
    	if( str_academyCode == null ){
    		str_academyCode = (String)session.getAttribute("academy_id");
    	}
    	user_log.setAcademia(Academy.instance(str_academyCode));
    	Validate.user_log_t  = user_log;
		session.setAttribute("ses_usuario", user_log);
		request.setAttribute("lst_minuta", Minuta.list(Validate.user_log_t));
		session.setAttribute("academy_id", str_academyCode);
		return mapping.findForward("default");
	}
    
    public ActionForward searchUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	DynaActionForm userSearch_form = (DynaActionForm) form;
    	Map userSearch = userSearch_form.getMap();
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	request.setAttribute("req_users", Usuario.buscar(userSearch, Validate.user_log_t.getAcademia().getCodigo()));
    	return mapping.findForward("default");
    }
    
    public ActionForward newDraft(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	DynaActionForm minuta_form = (DynaActionForm) form;
    	Map mapa_add;
    	mapa_add = minuta_form.getMap();
    	if( Minuta.add(mapa_add, Validate.user_log_t.getAcademia().getCodigo()) > 0 ){
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
    		return mapping.findForward("save_success");    		
    	}else{
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
    		return mapping.findForward("save_fail");
    	}
	}
    
    public ActionForward deleteDraft(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	DynaActionForm minuta_form = (DynaActionForm) form;
    	Map mapa_delete;
    	mapa_delete = minuta_form.getMap();
    	if( Minuta.delete(mapa_delete) > 0 ){
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
    	}else{
    		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailDelete"));
    	}
    	return mapping.findForward("default");
    }
    
    
    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward saveDraft(
    		ActionMapping mapping, 
    		ActionForm form, 
    		HttpServletRequest request, 
    		HttpServletResponse response) throws Exception {
    	
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	DynaActionForm minuta_form = (DynaActionForm) form;
		Map map_save = new HashMap();
		map_save = minuta_form.getMap();
		Minuta.save(map_save);
		Validate.user_log_t = null;
		request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward("save_success");
	}
    
    
    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editDraft(
    		ActionMapping mapping, 
    		ActionForm form, 
    		HttpServletRequest request, 
    		HttpServletResponse response) throws Exception{
    	int int_min;
    	MinutaBean bn_minuta;
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	    	
    	DynaActionForm forma = (DynaActionForm) form;
    	HttpSession session = request.getSession();
    	
    	String str_min = request.getParameter("draft_id");
    	
    	if(str_min==null){
    		 bn_minuta = (MinutaBean)session.getAttribute("bean_minuta");
    		 int_min = bn_minuta.getInt_tk();
    	}else{
    		int_min = Integer.parseInt(str_min);
    	}    	    	  

    	bn_minuta = (MinutaBean) Draft.edit(int_min);
    	forma.set("int_min", new Integer(int_min));
    	forma.set("str_expediente", bn_minuta.getStr_expediente());
    	forma.set("str_asunto", bn_minuta.getStr_asunto());
    	forma.set("str_lugar", bn_minuta.getStr_lugar());
    	forma.set("int_responsable", new Integer(bn_minuta.getInt_responsable()));
    	forma.set("str_responsable", bn_minuta.getStr_responsable());
    	forma.set("str_fechaReunion", bn_minuta.getStr_fechaReunion());
    	forma.set("str_horaReunion", bn_minuta.getStr_horaReunion());
    	forma.set("str_horaInicio", bn_minuta.getStr_horaInicio());
    	forma.set("str_horaFin", bn_minuta.getStr_horaFin());
    	forma.set("str_ordenDia", bn_minuta.getStr_ordenDia());
    	forma.set("int_numeroReunion", new Integer(bn_minuta.getStr_numeroReunion()));
    	forma.set("str_observaciones", bn_minuta.getStr_observaciones());
    	if(bn_minuta.getDate_reunionProx()!=null) forma.set("str_fechaProxima", bn_minuta.getStr_reunionProx());
    	forma.set("str_lugarProximo", bn_minuta.getStr_LugarProx());
    	forma.set("str_horaProxima", bn_minuta.getStr_horaProx());
    	forma.set("str_cerrada", bn_minuta.getStr_cerrada() );
    	    	
    	session.setAttribute("bean_minuta", bn_minuta);
		return mapping.findForward("default");
	}
    
    public ActionForward addObservation(
    		ActionMapping mapping, 
    		ActionForm form, 
    		HttpServletRequest request, 
    		HttpServletResponse response) throws Exception{

    	HttpSession session = request.getSession();
    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	DynaActionForm forma = (DynaActionForm) form;
    	
    	MinutaBean bn_minuta = (MinutaBean)session.getAttribute("bean_minuta");
    	forma.set("str_observaciones", bn_minuta.getStr_observaciones());
    	forma.set("str_fechaProxima", bn_minuta.getStr_reunionProx());
    	forma.set("str_lugarProximo", bn_minuta.getStr_LugarProx());
    	forma.set("str_horaProxima", bn_minuta.getStr_horaProx());
    	
		return mapping.findForward("default");
	}       
    
    public ActionForward saveObservation(
    		ActionMapping mapping, 
    		ActionForm form, 
    		HttpServletRequest request, 
    		HttpServletResponse response) throws Exception{

	    	ActionForward action_retVal = Validate.validateSession(mapping,request, getResources(request));
	    	if(action_retVal!=null)return action_retVal;
	    	DynaActionForm forma = (DynaActionForm) form;	    	    
	    	
	    	HttpSession session = request.getSession();
	    	MinutaBean bn_minuta = (MinutaBean)session.getAttribute("bean_minuta");
	    	int minuta_id = bn_minuta.getInt_tk();
	    	
	    	Map mapa_forma = forma.getMap();	
	    	Minuta.observation_save(mapa_forma, minuta_id);
			Validate.user_log_t = null;
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
			return mapping.findForward("save_success");	    	
	}       
    
}